#include "admin.h"
#include "userdb.h"
#include "bookdb.h"
#include "login.h"

using namespace Login;
using namespace User;
using Database::bookData;
extern Database::userDatabase &userdb;
extern Database::bookDatabase &bookdb;

admin::admin(string name) : user(name)
{

}

void admin::start()
{
	char operation;
	system("cls");
	cout << "Hello, administrator " << username << endl;
	for (int i = 0; i <= 150000000; i++);
	do
	{
		cout << "1.List all the users" << endl;
		cout << "2.Create new administrator" << endl;
		cout << "3.Remove a user" << endl;
		cout << "4.List all the books" << endl;
		cout << "5.Add a book" << endl;
		cout << "6.Remove a book" << endl;
		cout << "7.Clear the book database" << endl;
		cout << "8.Other" << endl;
		cout << "9.Quit" << endl;
		cin.sync();
		operation = _getch();
		switch (operation)
		{
		case '1':
			userdb.listUsers(); break;
		case '2':
			createAdmin(); break;
		case '3':
			removeUser(); break;
		case '4':
			bookdb.listBooks(); break;
		case '5':
			addBook(); break;
		case '6':
			removeBook(); break;
		case '7':
			clearBookDb(); break;
		case '8':
			user::start(); break;
		case '9': case 27:
			return;
		}
		system("cls");
	} while (true);
}

void admin::createAdmin()
{
	string username;
	system("cls");
	cout << "Username:";
	cin >> username;
	cout << "Password:";
	char *password = getPassword();
	unsigned char *cipher = getMD5(password);
	delete password;
	cin.get();
	int result = userdb.addUser(username, cipher, 1);
	if (result == 0)
	{
		cout << "The username is already exist." << endl;;
		for (int i = 0; i <= 200000000; i++);
	}
	else if (result == 1)
	{
		cout << "Create successfully." << endl;
		for (int i = 0; i <= 200000000; i++);
	}
}

void admin::removeUser()
{
	string name;
	int result;
	cout <<endl<< "The username:";
	cin >> name;
	if (username == name)
		cout << "You can't remove yourself." << endl;
	else
	{
		result = userdb.deleteUser(name);
		if (result == 0)
			cout << "Operation complete." << endl;
		else if (result == -1)
			cout << "Couldn't find the user \"" << name << "\"." << endl;
		else
			cout << "The user has " << result << (result == 1 ? " book " : " books ") << "that is borrowed out." << endl;
	}
	_getch();
}

void admin::addBook()
{
	bookData temp;
	string name;
	cout <<endl<< "The name:";
	getline(cin, name);
	temp.bookName = name;
	cout << "The number:";
	cin >> temp.totalNumber;
	bookdb += temp;
	_getch();
}

void admin::removeBook()
{
	string name;
	int number, result;
	cout <<endl<< "The name:";
	getline(cin, name);
	cout << "The number of book:";
	cin >> number;
	result=bookdb.deleteBook(name, number);
	if (result == -2147483647)
		cout << "Couldn't find the book \""<<name<<"\"." << endl;
	else if (result < 0)
		cout << "There are only " << -result << (result == -1 ? " book " : " books ") << "valid in the library." << endl;
	else
	{
		cout << "Operation complete." << endl;
		if (result > 0)
			cout << "Now there are " << result << ((result == 1) ? " book " : " books ") << "." << endl;
		else
			cout << "Now there aren't any book named \"" << name << "\"." << endl;
	}
	_getch();
}

void admin::clearBookDb()
{
	cout << "\nAre you sure to clear the book database? ";
	char operation = cin.get();
	int result;
	if (operation != 'y')
		return;
	cout << "Input your password:";
	unsigned char *cipher = Login::getMD5(Login::getPassword());
	result = userdb.check(username, cipher);
	if (result == 2)
	{
		bookdb.clearDb();
		cout << "Operation completed." << endl;
	}
	else
	{
		cout << "Wrong password." << endl;
	}
	_getch();
}